package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.impl.StudentServiceImpl;
import com.itheima.utils.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    private StudentServiceImpl studentService = new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求乱码问题
        req.setCharacterEncoding("utf-8");
        //响应乱码问题
        resp.setContentType("text/html;charset=utf-8");

        String action = req.getParameter("action");
        if (action.equals("findByPage")){
            findByPage(req,resp);
        }else if (action.equals("addStu")){
            addStu(req,resp);
        }else if (action.equals("updateStu")){
            updateStu(req,resp);
        }else if(action.equals("deleteStu")){
            deleteStu(req,resp);
        }


    }
    /*删除血学生信息*/
    private void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {


        String number = req.getParameter("number");
        System.out.println(number);
        //调用service层访问da层
        Integer count=studentService.deleteStu(number);

        if (count>0){
            //修改成功
            resp.getWriter().write("true");
        }else {
            //修改失败
            resp.getWriter().write("false");
        }
    }

    /*修改学生信息*/
    private void updateStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student = JsonUtil.req2Bean(req, Student.class);
        Integer count=studentService.updateStu(student);
        if (count>0){
            //修改成功
            resp.getWriter().write("true");
        }else {
            //修改失败
            resp.getWriter().write("false");
        }
    }

    /*添加学生*/
    private void addStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接收学生信息，并封装
        Student student = JsonUtil.req2Bean(req, Student.class);
        //调用service层访问dao
        Integer count = studentService.addStu(student);
        if (count>0){
            //添加成功
            resp.getWriter().write("true");
        }else {
            //添加失败
            resp.getWriter().write("false");
        }

    }
    /*查询*/
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPageStr = req.getParameter("currentPage");
        String pageSizeStr = req.getParameter("pageSize");
        int currentPage=Integer.parseInt(currentPageStr);
        int pageSize= Integer.parseInt(pageSizeStr);
        //使用service调用dao层

        PageInfo<Student> pageInfo=studentService.findByPage(currentPage,pageSize);
        String jsonStr = JsonUtil.obj2JsonStr(pageInfo);
        resp.getWriter().write(jsonStr);
    }
}
